﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; 
using SHKF.B2C.WanLiNiu.Model;
using SHKF.B2C.WanLiNiu.Model.Inter.InventorySyn;
using SHKF.B2C.WanLiNiu.Model.Inter.OpenTradeListTrades;
using SHKF.B2C.WanLiNiu.Model.Inter.StorageQuery;
using SHKF.B2C.WanLiNiu.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace SHKF.B2C.JuShuiTan.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly HttpClientWLN httpClient;

        public TestController(HttpClientWLN httpClient)
        {
            this.httpClient = httpClient;
        }

        public async Task Get()
        {
            StringBuilder htmlStringBuilder = new StringBuilder();
            htmlStringBuilder.Append("<html>");
            htmlStringBuilder.Append("<head><meta http-equiv=\"Content-Type\" content=\"charset=utf-8\" />");//支持中文
            htmlStringBuilder.Append("<style>");
            htmlStringBuilder.Append(".area { margin-left:16px; margin-top:16px; margin-bottom:20px;}");
            htmlStringBuilder.Append(".table { margin-left:16px; }");
            htmlStringBuilder.Append("td { border-bottom: 1px solid #808080;padding:8px }");
            htmlStringBuilder.Append("</style>");
            htmlStringBuilder.Append("<body>");
            htmlStringBuilder.Append("<ol style=\"font - size: 300 % \">");//让字体变大　
            string datahtml = $"WanLiNiu hello";

            List<string> list = new List<string>() { };
            list.Add(GetList());           //erp/open/inventory/uploadinventory/deduct  库存上传到平台时将被扣减的库存
            list.Add(ProductUpload());     //erp/open/inventory/syn                      库存全量同步
            list.Add(InventoryUpload());   //erp/opentrade/list/trades                    查询订单

            list.ForEach(x =>
            {
                datahtml += "<li style='margin: 20px;color:blue;margin-top:32px'>" + x + "</li>";
            });

            htmlStringBuilder.Append(datahtml);
            htmlStringBuilder.Append("</ol>");
            htmlStringBuilder.Append("</body>");
            htmlStringBuilder.Append("</html>");
            string result = htmlStringBuilder.ToString();
            var data = Encoding.UTF8.GetBytes(result);

            Response.ContentType = "text/html";

            await Response.Body.WriteAsync(data, 0, data.Length);
        }

        private string GetList()
        {
            string html = "获取线上出库订单 Post: http://43.138.6.61:5002/order/list <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "OrdersSingleParam", ColumnType = "类", Remark = "&nbsp;" }); 
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "OrderDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }

        private string ProductUpload()
        {
            string html = "上架产品 Post: http://43.138.6.61:5002/product/upload <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "ItemSkuParam", ColumnType = "类", Remark = "&nbsp;" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "BaseDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }

        private string InventoryUpload()
        {
            string html = "新建盘点单 Post: http://43.138.6.61:5002/inventory/upload <br>";

            List<Row> rowsIn = new List<Row>();
            rowsIn.Add(new Row() { ColumnName = "InventoryUploadParam", ColumnType = "类", Remark = "&nbsp;" });
            string strRows = this.GetIn(rowsIn);

            List<Row> rowsOut = new List<Row>();
            rowsOut.Add(new Row() { ColumnName = "InventoryUploadDto", ColumnType = "类", Remark = "&nbsp;" });

            strRows += this.GetOut(rowsOut);

            return html + strRows;
        }


        private string GetIn(List<Row> columns)
        {
            string html = "<div class=\"area\">入参:" +
                "<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
            html += this.GetColumnHeader();

            foreach (Row column in columns)
            {
                html += this.GetRow(column.ColumnName, column.ColumnType, column.Remark);
            }

            html += "</table></div>";
            return html;
        }
        private string GetOut(List<Row> columns)
        {
            string html = "<div class=\"area\">出参:" +
                "<table class=\"table\" cellpadding=\"0\" cellspacing=\"0\">";
            html += this.GetColumnHeader();

            foreach (Row column in columns)
            {
                html += this.GetRow(column.ColumnName, column.ColumnType, column.Remark);
            }

            html += "</table></div>";
            return html;
        }
        private string GetColumnHeader()
        {
            return $"<tr>" +
                $"<td class=\"header\">列名</td>" +
                $"<td class=\"header\">类型</td>" +
                $"<td class=\"header\">备注</td>" +
                $"</tr>";
        }
        private string GetRow(string columnName, string strType, string remark)
        {
            return $"<tr>" +
                $"<td>{columnName}</td>" +
                $"<td>{strType}</td>" +
                $"<td>{remark}</td>" +
                $"</tr>";
        }

        [HttpGet("Storage")]
        public async Task<StorageDto> Storage(int page_no, int page_size)
        {
            try
            {
                StorageDto dto = await this.httpClient.StorageQuery(page_no, page_size);

                return dto;
            }
            catch (Exception ex)
            {  
                throw ex;
            } 
        }

        [HttpPost("InventorySyn")]
        public async Task<InventorySynDto> InventorySyn(InventorySynParam data)
        {
            try
            {
                InventorySynDto dto = await this.httpClient.InventorySyn(data);

                return dto;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        [HttpGet("OrderList")]
        public async Task<OpenTradeListDto> OrderList(int page, int limit, string storage_code, DateTime pay_time, DateTime end_time)
        {
            long temp = DateTimeUtil.DateTimeToTimeStamp(DateTime.Now.AddMonths(-2)); 

            //OrderQueryParam data = new OrderQueryParam();
            //data.page = page;
            //data.limit = limit;
            //data.storage_code = storage_code;
            //data.pay_time = DateTimeUtil.DateTimeToTimeStamp(pay_time);
            //data.end_time = DateTimeUtil.DateTimeToTimeStamp(end_time);
            //data.create_time = temp;
            //data.modify_time = temp;
            //data.approve_time = temp;
            //data.send_goods_time = temp;
            //data.finish_time = temp;
            try
            {
                Dictionary<string, object> data = new Dictionary<string, object>();
                data.Add("page", page);
                data.Add("limit", limit);
                data.Add("storage_code", storage_code);
                data.Add("pay_time", DateTimeUtil.DateTimeToLongTimeStamp(pay_time));
                data.Add("end_time", DateTimeUtil.DateTimeToLongTimeStamp(end_time));

                OpenTradeListDto dto = await this.httpClient.OrderList(data); 
                return dto;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
